// 设置分类模型和文章模型的关联
const Categorys = require('../models/Categorys')
const Post = require('../models/Post')
const Tags = require('../models/Tags')
const PostTag = require('../models/PostTag')
const Users = require('../models/Users')
const Comments = require('../models/Comments')

Categorys.hasMany(Post, { as: 'posts', foreignKey: 'categoryId' })
Post.belongsTo(Categorys, { as: 'categorys', foreignKey: 'categoryId' })

// models/Post.js
Post.belongsToMany(Tags, {
  through: 'post_tags', // 指定关联表
  foreignKey: 'postId', // 指向 posts 的外键
  as: 'postTags',
})

// models/Tag.js
Tags.belongsToMany(Post, {
  through: 'post_tags', // 同一张关联表
  foreignKey: 'tagId', // 指向 tags 的外键
})
//post_tags is not associated to posts!
Post.hasMany(PostTag, { foreignKey: 'postId', as: 'post_tags' })
Tags.hasMany(PostTag, { foreignKey: 'tagId', as: 'post_tags' })
PostTag.hasMany(Post, { as: 'posts', foreignKey: 'id' })
PostTag.hasMany(Tags, { as: 'tags', foreignKey: 'id' })

Post.belongsTo(Users, { as: 'users', foreignKey: 'userId' })
Users.hasMany(Post, { as: 'posts', foreignKey: 'userId' })

Comments.belongsTo(Post, { foreignKey: 'postId' })
Comments.belongsTo(Comments, {
  as: 'Parent',
  foreignKey: 'parentId',
})

Comments.hasMany(Comments, {
  as: 'parentComment',
  foreignKey: 'parentId',
})
// 文章表关联标签表
